//
//  ICSearchBar.m
//  ICome
//
//  Created by ENN on 16/5/11.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICSearchBar.h"
#import "ICHeaderFile.h"
#import "UIImage+Extension.h"
@implementation ICSearchBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.font  = [UIFont systemFontOfSize:14];
        self.placeholder = @"搜索";
        self.backgroundColor   = [UIColor whiteColor];
        self.iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 10)];
        self.iconImage.image        = [UIImage  imageNamedFromiComeSDKBundle:@"icon_search"];
        self.iconImage.width        = 24;
        self.iconImage.height       = 10;
        self.iconImage.contentMode  = UIViewContentModeRight;
        
        self.spaceLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 31, 10)];
        [self.spaceLeftView addSubview:self.iconImage];
        
        [self.iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.spaceLeftView.mas_top);
            make.right.equalTo(self.spaceLeftView.mas_right).offset(-7);
            make.height.mas_equalTo(10);
        }];
        
        self.leftView          = self.spaceLeftView;
        self.leftViewMode      = UITextFieldViewModeAlways;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius  = 4;
        self.returnKeyType     = UIReturnKeySearch;
        self.tintColor = ICSEARCHCANCELCOLOR;
       
        self.rightViewMode = UITextFieldViewModeWhileEditing;//必不可少
        self.rightView = self.clearButton;
    }
    return self;
}

+ (instancetype)searchBar
{
    return [[self alloc] init];
}

-(UIButton *)clearButton {
    if (!_clearButton) {
        _clearButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,30,30)];
        [_clearButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_message_search_delete"] forState:UIControlStateNormal];
        [_clearButton addTarget:self action:@selector(clearSearchBar) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}

- (void)clearSearchBar {
    if(self.editing)
    {
        [self setText:@""];
    }
}






@end
